package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.SystemColor;

import DBLayer.*;
import ModelLayer.*;
import ControlLayer.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.border.TitledBorder;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Choice;

import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import java.awt.Button;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JSeparator;

import java.awt.Canvas;
import java.awt.Checkbox;

import javax.swing.Box;

import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class homeView extends JFrame {

	private JPanel contentPane;
	private DBCustomer dbCustomer;
	private CtrCustomer ctrCustomer;
	private CtrOffer ctrOffer;
	private CtrMaterial ctrMaterial;
	private CtrLabour ctrLabour;
	private DBLabour dbLabour;
	private BDMaterial dbMaterial;
	private DBOffer dbOffer;
	private JComboBox<Customer> comboBox;
	private JComboBox<Labour> labourComboBox;
	private DBOfferMaterial dbOfferMaterial;
	private DBOfferLabour dbOfferlabour;
	private CtrOfferLabour ctrOfferLabour;
	private CtrOfferMaterial ctrOfferMaterial;
	private JComboBox<Material> comboBoxMaterial;
	private JTable customerTableList;
	private ArrayList<Customer> customerList;
	private  DefaultTableModel  customerTableModel;
	private int i;
	private ArrayList<Labour> labourList;
	private DefaultTableModel labourTableModel;
	private int o;	
	private JTable materialsTableList;
	private JTable labourTableList;
	private ArrayList<Material> materialList;
	private DefaultTableModel materialTableModel;
	private int p;
	private JTable offerTableList;
	private ArrayList<Offer> offerList;
	private DefaultTableModel offerTableModel;
	private int a;
	private ArrayList<OfferLabour> offerLabours;
	private int s;
	private JTable offerTableList1;
	private DefaultTableModel offerLabourTableModel;
	private ArrayList<OfferMaterial> offerMaterials;
	private int d;
	private DefaultTableModel offerMaterialTableModel;
	private JTable offerTableList2;
	private Checkbox checkbox_1;
	private TextField textField_materialAmount;
	private TextField textField_materialPrice;
	private JButton product_add;
	private JButton button_15;
	private JButton product_delete;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					homeView frame = new homeView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public homeView() {
		ctrCustomer = new CtrCustomer();
		ctrOffer = new CtrOffer();
		ctrMaterial = new CtrMaterial();
		dbCustomer = new DBCustomer();
		ctrLabour = new CtrLabour();
		dbMaterial = new BDMaterial();
		dbLabour = new DBLabour();
		dbOffer = new DBOffer();
		dbOfferMaterial = new DBOfferMaterial();
		dbOfferlabour = new DBOfferLabour();
		ctrOfferLabour = new CtrOfferLabour();
		ctrOfferMaterial = new CtrOfferMaterial();
		
		
		
		customerList = dbCustomer.getAllCustomer();
		labourList = dbLabour.getAllLabour();
		materialList = dbMaterial.getAllMaterials();
		offerList = dbOffer.getAllOffers();
		setTitle("GiV Construcions Manager");
		setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\Irena\\BGM.Irena\\BGM.Irena.ClientApp\\BGM.Irena.Client\\Resources\\Trailers.png"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//setBounds(100, 100,1024, 9000);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.activeCaption);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.activeCaption);
		panel.setBorder(new TitledBorder(null, "Offers", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(16, 28, 228, 673);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(10, 23, 208, 639);
		panel.add(scrollPane);
		
		
		 
	      
	      
	      JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	      tabbedPane.setBounds(254, 32, 1022, 669);
	      contentPane.add(tabbedPane);
	      
	      JPanel panel_1 = new JPanel();
	      panel_1.setBackground(SystemColor.inactiveCaption);
	      tabbedPane.addTab("Offer", null, panel_1, null);
	      panel_1.setLayout(null);
	      
	      JPanel panel_2 = new JPanel();
	      panel_2.setBackground(SystemColor.activeCaption);
	      panel_2.setBorder(new TitledBorder(null, "Offer", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_2.setBounds(6, 44, 486, 129);
	      panel_1.add(panel_2);
	      panel_2.setLayout(null);
	      
	       
	       comboBox = new JComboBox(customerList.toArray());
	       comboBox.setBounds(83, 51, 212, 20);
	       panel_2.add(comboBox);
	      
	      Label label = new Label("Offer ID:");
	      label.setBounds(10, 23, 47, 22);
	      panel_2.add(label);
	      
	      final TextField textField_offerId = new TextField();
	      textField_offerId.setBounds(83, 23, 135, 22);
	      panel_2.add(textField_offerId);
	      
	      Label label_2 = new Label("Customer:");
	      label_2.setBounds(10, 49, 62, 22);
	      panel_2.add(label_2);
	      
	    
	      
	      Label label_14 = new Label("Including");
	      label_14.setBounds(10, 77, 62, 22);
	      panel_2.add(label_14);
	      
	      JSeparator separator_2 = new JSeparator();
	      separator_2.setBounds(63, 89, 413, 2);
	      panel_2.add(separator_2);
	      
	      Label label_24 = new Label("Offer date:");
	      label_24.setBounds(224, 23, 62, 22);
	      panel_2.add(label_24);
	      
	      final TextField textField_offerDate = new TextField();
	      textField_offerDate.setBounds(291, 23, 95, 22);
	      panel_2.add(textField_offerDate);
	     
	      
	      Object[][] offerData = null;
	      Object[] columnsNamesOffer = {"Offer Id", "Customer", "Date"};
	      offerTableModel = new DefaultTableModel(offerData,columnsNamesOffer);
	      offerTableList = new JTable(offerTableModel);
	      offerTableList.addMouseListener(new MouseAdapter() {
	      	@Override
	      	public void mouseClicked(MouseEvent e) 
	      	{
	      		int row = offerTableList.getSelectedRow();
	      		int offerId = Integer.parseInt(String.valueOf(offerTableModel.getValueAt(row, 0)));
	      		ctrOffer.findOffer(offerId);
	      		textField_offerId.setText(Integer.toString(offerId));
	      		textField_offerDate.setText(ctrOffer.findOffer(offerId).getOfferDate());
	      		offerLabours = dbOfferlabour.getAllOfferLaboursConnectedToOffer(offerId);
	      		offerMaterials = dbOfferMaterial.getAllOfferMaterialsConnectedToOffer(offerId);
	      		offerLabourTableModel.setRowCount(0);
	      		offerMaterialTableModel.setRowCount(0);
	      		checkbox_1.setState(false);
	      		textField_materialAmount.setEditable(false);
      			textField_materialAmount.setEnabled(false);
      			textField_materialPrice.setEditable(false);
      			textField_materialPrice.setEnabled(false);
      			product_add.setEnabled(false);
      			button_15.setEnabled(false);
      			product_delete.setEnabled(false);
      			comboBoxMaterial.setEnabled(false);
	      		
	      		
	      		
	      		 for( s = 0; s < offerLabours.size();s++)
	      		{
	   	    	  offerLabourTableModel.insertRow(s, new Object[]{offerLabours.get(s).getOfferLabourId(),offerLabours.get(s).getLabourName(), offerLabours.get(s).getLabourPrice(),offerLabours.get(s).getLabourAmount()});
	      		System.out.println("LABOUR");
	      		}
	      		 
	      		for( d = 0; d < offerMaterials.size();d++)
		      		{
	      			
		   	    	  offerMaterialTableModel.insertRow(d, new Object[]{offerMaterials.get(d).getOfferMaterialId(),offerMaterials.get(d).getMaterialName(),offerMaterials.get(d).getMaterialType(), offerMaterials.get(d).getMaterialPrice(), offerMaterials.get(d).getMaterialQuantity()});
		   	    	
		      		if(d > 0)
		      		{
		      			checkbox_1.setState(true);
		      			textField_materialAmount.setEditable(true);
		      			textField_materialAmount.setEnabled(true);
		      			textField_materialPrice.setEditable(true);
		      			textField_materialPrice.setEnabled(true);
		      			product_add .setEnabled(true);
		      			button_15.setEnabled(true);
		      			product_delete.setEnabled(true);
		      			comboBoxMaterial.setEnabled(true);
		      			
		      		}
		      		
		      		}
	      	}
	      });
	      scrollPane.setViewportView(offerTableList);
	      for( a = 0; a < offerList.size();a++)
	      {
	    	  offerTableModel.insertRow(a, new Object[]{offerList.get(a).getOfferId(),ctrCustomer.findCustomer(offerList.get(a).getCustomerId()).getCustomerSurname()+" "+ctrCustomer.findCustomer(offerList.get(a).getCustomerId()).getCustomerName(),offerList.get(a).getOfferDate()});
	      }
	      
	      JPanel panel_3 = new JPanel();
	      panel_3.setBackground(SystemColor.activeCaption);
	      panel_3.setBorder(new TitledBorder(null, "Offer Materials", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_3.setBounds(502, 44, 505, 586);
	      panel_1.add(panel_3);
	      panel_3.setLayout(null);
	      
	      final TextField textField_3 = new TextField();
	      textField_3.setText("INFO: Database connection-OK, Ready to work.");
	      textField_3.setForeground(new Color(255, 0, 0));
	      textField_3.setEditable(false);
	      textField_3.setBounds(254, 707, 1022, 22);
	      contentPane.add(textField_3);
	      
	     
	      
	      comboBoxMaterial = new JComboBox(materialList.toArray());
	      comboBoxMaterial.setEnabled(false);
	      comboBoxMaterial.setBounds(10, 26, 368, 20);
	      panel_3.add(comboBoxMaterial);


	      textField_materialPrice = new TextField();
	      textField_materialPrice.setEnabled(false);
	      textField_materialPrice.setEditable(false);
	      textField_materialPrice.setBounds(54, 54, 46, 22);
	      panel_3.add(textField_materialPrice);
	      
	      textField_materialAmount = new TextField();
	      textField_materialAmount.setEnabled(false);
	      textField_materialAmount.setEditable(false);
	      textField_materialAmount.setBounds(167, 54, 62, 22);
	      panel_3.add(textField_materialAmount);
	      
	      Icon new2 = new ImageIcon("Images/icon_new.png");
	      product_add = new JButton(new2); 
	      product_add.setEnabled(false);
	      product_add.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int orderId = Integer.valueOf(textField_offerId.getText());
	      		Material material = (Material) comboBoxMaterial.getSelectedItem();
	      		int materialId = (int) material.getMaterialId();
	      		int quantity = Integer.valueOf(textField_materialAmount.getText());
	      		double price = Double.valueOf(textField_materialPrice.getText());
	      		Offer offer = dbOffer.findOffer(orderId);
	      		String type = material.getMaterialType();
	      		String name = material.getMaterialName();
	      		dbOfferMaterial.generateId();
	      		int offerMaterialId = dbOfferMaterial.getGeneratedId();
	      		System.out.println(offerMaterialId);
	      		ctrOffer.addOfferMaterial(offer, price, quantity, type, materialId, name, offerMaterialId);
	      		ctrOfferMaterial.insertNewOfferMaterial(price, quantity, orderId, type, materialId, name, offerMaterialId);
	      		offerMaterialTableModel.insertRow(d, new Object[]{offerMaterialId, name, type, price, quantity});
	      		d= offerMaterialTableModel.getRowCount();
	      		textField_materialAmount.setText(null);
	      		textField_materialPrice.setText(null);
	      		textField_3.setText("Material was added to the offer");
	      		
	      		
	      	}
	      });
	      product_add.setToolTipText("Add material");
	      product_add.setBackground(SystemColor.activeCaption);
	      product_add.setBounds(388, 26, 34, 22);
	      panel_3.add(product_add);
	      
	      Icon delete1 = new ImageIcon("Images/ico_down.png");
	      product_delete = new JButton(delete1); 
	      product_delete.setEnabled(false);
	      product_delete.setBackground(SystemColor.activeCaption);
	      product_delete.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		try{
		      		int row = offerTableList2.getSelectedRow();
		      		int offerMaterialId = Integer.parseInt(String.valueOf(offerMaterialTableModel.getValueAt(row, 0)));
		      		ctrOfferMaterial.deleteOfferMaterial(offerMaterialId);
		      		offerMaterialTableModel.removeRow(row);
		      		d= d-1;
		      		textField_3.setText("Material was removed from the offer.");
		      	}catch(Exception q){textField_3.setText("You must select material from material list to delete it, please try again.");
	      	}
	      	}
	      });
	      product_delete.setBounds(452, 26, 38, 22);
	      panel_3.add(product_delete);
	      
	      Label label_13 = new Label("Price:");
	      label_13.setBounds(10, 54, 38, 22);
	      panel_3.add(label_13);
	      
	      
	      Icon update_offerMaterial = new ImageIcon("Images/disk_red.png");
	      button_15 = new JButton(update_offerMaterial);
	      button_15.setEnabled(false);
	      button_15.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int row = offerTableList2.getSelectedRow();
	      		int orderId = Integer.valueOf(textField_offerId.getText());
	      		Material material = (Material) comboBoxMaterial.getSelectedItem();
	      		int materialId = (int) material.getMaterialId();
	      		int offerMaterialId = Integer.parseInt(String.valueOf(offerMaterialTableModel.getValueAt(row, 0)));
	      		String name = ctrOfferMaterial.findOfferMaterialById(offerMaterialId).getMaterialName();
	      		int quantity = Integer.valueOf(textField_materialAmount.getText());
	      		Double price = Double.valueOf(textField_materialPrice.getText());
	      		String type = ctrOfferMaterial.findOfferMaterialById(offerMaterialId).getMaterialType();
	      		ctrOfferMaterial.updateOfferMaterial(price, quantity, orderId, type, materialId, name, offerMaterialId);
	      		offerMaterialTableModel.setValueAt(name, row , 1);
	      		offerMaterialTableModel.setValueAt(type, row , 2);
	      		offerMaterialTableModel.setValueAt(price, row , 3);
	      		offerMaterialTableModel.setValueAt(quantity, row , 4);
	      		textField_3.setText("Offer material was updated");
	      	}
	      });
	      button_15.setBackground(SystemColor.activeCaption);
	      button_15.setBounds(417, 26, 37, 22);
	      panel_3.add(button_15);
	      
	      Label label_23 = new Label("Amount:");
	      label_23.setBounds(106, 54, 54, 22);
	      panel_3.add(label_23);
	      
	      JScrollPane scrollPane_5 = new JScrollPane();
	      scrollPane_5.setBounds(10, 90, 485, 485);
	      panel_3.add(scrollPane_5);
	      
	      
	      Object[][] materialOfferData = null;
	      Object[] columnsNamesOfferMaterials = {"Material Id", "Name","Type", "Price", "Amount"};
	      offerMaterialTableModel = new DefaultTableModel(materialOfferData,columnsNamesOfferMaterials);
	      offerTableList2 = new JTable(offerMaterialTableModel);
	      offerTableList2.addMouseListener(new MouseAdapter() {
	      	@Override
	      	public void mouseClicked(MouseEvent arg0)
	      	{
	      		int row = offerTableList2.getSelectedRow();
	      		int offerMaterialId = Integer.parseInt(String.valueOf(offerTableList2.getValueAt(row, 0)));
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		ctrOfferMaterial.findOfferMaterial(offerId);
	      		textField_materialAmount.setText(String.valueOf(ctrOfferMaterial.findOfferMaterialById(offerMaterialId).getMaterialQuantity()));
	      		textField_materialPrice.setText(String.valueOf(ctrOfferMaterial.findOfferMaterialById(offerMaterialId).getMaterialPrice()));
	      	}
	      });
	      scrollPane_5.setViewportView(offerTableList2);
	      
	     
	      
	      JPanel panel_4 = new JPanel();
	      panel_4.setBackground(SystemColor.activeCaption);
	      panel_4.setBorder(new TitledBorder(null, "Offer Labour", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_4.setBounds(6, 184, 486, 446);
	      panel_1.add(panel_4);
	      panel_4.setLayout(null);
	      
	      
	      labourComboBox = new JComboBox(labourList.toArray());
	      labourComboBox.setBounds(10, 22, 359, 20);
	      panel_4.add(labourComboBox);
	      
	      Label label_16 = new Label("Price:");
	      label_16.setBounds(10, 48, 41, 22);
	      panel_4.add(label_16);
	      
	      final TextField textField_labourPrice = new TextField();
	      textField_labourPrice.setBounds(57, 48, 46, 22);
	      panel_4.add(textField_labourPrice);
	      
	      Label label_22 = new Label("Amount:");
	      label_22.setBounds(109, 48, 46, 22);
	      panel_4.add(label_22);
	      
	      final TextField textField_labourAmount = new TextField();
	      textField_labourAmount.setBounds(161, 48, 70, 22);
	      panel_4.add(textField_labourAmount);
	      
	      Icon remove_labour = new ImageIcon("Images/ico_down.png");
	      JButton button_12 = new JButton(remove_labour);
	      button_12.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		try{
	      		int row = offerTableList1.getSelectedRow();
	      		int offerLabourId = Integer.parseInt(String.valueOf(offerLabourTableModel.getValueAt(row, 0)));
	      		ctrOfferLabour.deleteOfferLabour(offerLabourId);
	      		offerLabourTableModel.removeRow(row);
	      		
	      		s=s-1;
	      		System.out.println("INDEX "+ s);
	      		textField_3.setText("Labour was removed from the offer.");
	      	}catch(Exception q){textField_3.setText("You must select labour from labour list to delete it, please try again.");
	      	
	      	}
	      		
	      	}
	      });
	      button_12.setToolTipText("Remove labour from offer");
	      button_12.setBackground(SystemColor.activeCaption);
	      button_12.setBounds(442, 22, 34, 22);
	      panel_4.add(button_12);
	      Icon update_offerLabour = new ImageIcon("Images/disk_red.png");
	      JButton button_13 = new JButton(update_offerLabour);
	      button_13.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int row = offerTableList1.getSelectedRow();
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		Labour labour = (Labour) labourComboBox.getSelectedItem();
	      		int labourId = (int) labour.getLabourID();
	      		//String name = labour.getLabourName();
	      		//dbOfferlabour.generateId();
	      		int offerLabourId = Integer.parseInt(String.valueOf(offerLabourTableModel.getValueAt(row, 0)));
	      		String name = ctrOfferLabour.findOfferLabourById(offerLabourId).getLabourName();
	      		int amount = Integer.valueOf(textField_labourAmount.getText());
	      		Double price = Double.valueOf(textField_labourPrice.getText());
	      		ctrOfferLabour.updateOfferLabour1(offerId, labourId, price, amount, name, offerLabourId);
	      		offerLabourTableModel.setValueAt(name, row , 1);
	      		offerLabourTableModel.setValueAt(price, row , 2);
	      		offerLabourTableModel.setValueAt(amount, row , 3);
	      		textField_3.setText("Offer labour was updated");
	      	}
	      });
	      button_13.setToolTipText("Update labour");
	      button_13.setBackground(SystemColor.activeCaption);
	      button_13.setBounds(411, 22, 34, 22);
	      panel_4.add(button_13);
	      
	      Icon add_offerLabour = new ImageIcon("Images/icon_new.png");
	      JButton button_14 = new JButton(add_offerLabour);
	      button_14.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		Labour labour = (Labour) labourComboBox.getSelectedItem();
	      		int labourId = (int) labour.getLabourID();
	      		String name = labour.getLabourName();
	      		dbOfferlabour.generateId();
	      		int offerLabourId = dbOfferlabour.getGeneratedId();
	      		int amount = Integer.valueOf(textField_labourAmount.getText());
	      		Double price = Double.valueOf(textField_labourPrice.getText());
	      		Offer offer = dbOffer.findOffer(offerId);

	      		ctrOffer.addOfferLabour(offer, price, amount, labourId, name, offerLabourId);
	      		ctrOfferLabour.insertNewOfferLabour(offerId, labourId, price, amount, name, offerLabourId);
	      		offerLabourTableModel.insertRow(s, new Object[]{offerLabourId, name, price, amount});
	      		s = offerLabourTableModel.getRowCount();
	      		textField_labourAmount.setText(null);
	      		textField_labourPrice.setText(null);
	      		textField_3.setText("Labour was added to the offer");
	      		
	      		
	      	}
	      });
	      button_14.setToolTipText("Add labour");
	      button_14.setBackground(SystemColor.activeCaption);
	      button_14.setBounds(379, 22, 34, 22);
	      panel_4.add(button_14);
	      
	      JScrollPane scrollPane_4 = new JScrollPane();
	      scrollPane_4.setBounds(10, 82, 466, 353);
	      panel_4.add(scrollPane_4);
	      
	      Object[][] offerLabourData = null;
	      Object[] columnsNamesLabour = {"Labour Id", "Name","Price","Amount"};
	      offerLabourTableModel = new DefaultTableModel(offerLabourData,columnsNamesLabour);
	      offerTableList1 = new JTable(offerLabourTableModel);
	      offerTableList1.addMouseListener(new MouseAdapter() {
	      	@Override
	      	public void mouseClicked(MouseEvent arg0)
	      	{
	      		int row = offerTableList1.getSelectedRow();
	      		int offerLabourId = Integer.parseInt(String.valueOf(offerTableList1.getValueAt(row, 0)));
	      		
	      		
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		ctrOfferLabour.findOfferLabour(offerId);
	      		textField_labourAmount.setText(String.valueOf(ctrOfferLabour.findOfferLabourById(offerLabourId).getLabourAmount()));
	      		textField_labourPrice.setText(String.valueOf(ctrOfferLabour.findOfferLabourById(offerLabourId).getLabourPrice()));
	      	
	      	}
	      });
	      scrollPane_4.setViewportView(offerTableList1);
	      
	      Icon new1 = new ImageIcon("Images/icon_new.png");
	      JButton offer_new = new JButton(new1); 
	      offer_new.setToolTipText("New offer");
	      offer_new.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e)
	      	{
	      		textField_offerId.setText(null);
	      		textField_offerDate.setText(null);
	      		textField_labourAmount.setText(null);
	      		textField_labourPrice.setText(null);
	      		offerLabourTableModel.setRowCount(0);
	      		offerMaterialTableModel.setRowCount(0);
	      		s=0;
	      		d=0;
	      	}
	      });
	      offer_new.setBackground(SystemColor.activeCaption);
	      offer_new.setBounds(6, 11, 34, 22);
	      panel_1.add(offer_new);
	      
	      Icon delete = new ImageIcon("Images/ico_down.png");
	      JButton offer_delete = new JButton(delete); 
	      offer_delete.setToolTipText("Delete offer");
	      offer_delete.setBackground(SystemColor.activeCaption);
	      offer_delete.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e)
	      	{
	      		try{
	      		int row = offerTableList.getSelectedRow();
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		offerTableModel.removeRow(row);
	      		
	      		offerLabourTableModel.setRowCount(0);
	      		offerMaterialTableModel.setRowCount(0);
	      		ctrOfferLabour.deleteOfferLabourConnected(offerId);
	      		s= s-s;
	      		ctrOfferMaterial.deleteOfferMaterialConnected(offerId);
	      		d= d-d;
	      		ctrOffer.deleteOffer(offerId);
	      		a = a - 1;
	      		textField_offerId.setText(null);
	      		textField_offerDate.setText(null);
	      		textField_labourAmount.setText(null);
	      		textField_labourPrice.setText(null);
	      		textField_3.setText("Order: "+offerId+" was deteted from the system");
	      		}catch(Exception q){textField_3.setText("You must select Offer from offer list to delete it, please try again.");
	      		
	      		}
	      	}
	      });
	      offer_delete.setBounds(106, 11, 34, 22);
	      panel_1.add(offer_delete);
	      
	      Icon save = new ImageIcon("Images/disk.png");
	      JButton offer_save = new JButton(save); 
	      offer_save.setToolTipText("Save offer");
	      offer_save.setBackground(SystemColor.activeCaption);
	      offer_save.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		Customer customer = (Customer) comboBox.getSelectedItem();
	      		int customerId = customer.getCustomerId();
	      		String date = String.valueOf(textField_offerDate.getText());
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		boolean existing = false;
				for ( int j = 0; j < dbOffer.getAllOffers().size() ; j++)
				{
					if ( dbOffer.getAllOffers().get(j).getOfferId() == offerId)
					{
					 existing = true;
					}
				}
				if(existing == false)
				{
	      		ctrOffer.createOffer(offerId, customerId, date);
	      		String surnameName = ctrCustomer.findCustomer(customerId).getCustomerSurname() + " " + ctrCustomer.findCustomer(customerId).getCustomerName();
	      	    offerTableModel.insertRow(a, new Object[]{offerId, surnameName, date});
	      		textField_3.setText("Offer with offer Id "+offerId+" was added to the system.");
				}
				else
				{
					textField_3.setText("OderId: " +offerId+ " is already used in the system, please select another id.");
				}
	      	}
	      });
	      offer_save.setBounds(40, 11, 34, 22);
	      panel_1.add(offer_save);
	      
	      Icon update = new ImageIcon("Images/disk_red.png");
	      JButton offer_update = new JButton(update); 
	      offer_update.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int row = offerTableList.getSelectedRow();
	      		Customer customer = (Customer) comboBox.getSelectedItem();
	      		int customerId = customer.getCustomerId();
	      		String date = String.valueOf(textField_offerDate.getText());
	      		int offerId = Integer.valueOf(textField_offerId.getText());
	      		ctrOffer.updateOffer(offerId, customerId, date);
	      		
	      		offerTableModel.setValueAt(date, row , 2);
	      		
	      	}
	      });
	      offer_update.setToolTipText("Update offer");
	      offer_update.setBackground(SystemColor.activeCaption);
	      offer_update.setBounds(73, 11, 34, 22);
	      panel_1.add(offer_update);
	      
	      Icon word = new ImageIcon("Images/printer.png");
	      JButton offer_generate = new JButton(word);
	      offer_generate.setToolTipText("Print");
	      offer_generate.setBackground(SystemColor.activeCaption);
	      offer_generate.setForeground(SystemColor.activeCaption);
	      offer_generate.setBounds(458, 11, 34, 22);
	      panel_1.add(offer_generate);
	      
	      JPanel panel_7 = new JPanel();
	      panel_7.setBackground(SystemColor.inactiveCaption);
	      tabbedPane.addTab("Customer", null, panel_7, null);
	      panel_7.setLayout(null);
	      
	      JPanel panel_8 = new JPanel();
	      panel_8.setBackground(SystemColor.activeCaption);
	      panel_8.setBorder(new TitledBorder(null, "New customer", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_8.setBounds(8, 38, 445, 150);
	      panel_7.add(panel_8);
	      panel_8.setLayout(null);
	      
	      Label label_4 = new Label("Customer Id:");
	      label_4.setBounds(10, 20, 78, 22);
	      panel_8.add(label_4);
	      
	      final TextField textField_customerId = new TextField();
	      textField_customerId.setBounds(94, 20, 82, 22);
	      panel_8.add(textField_customerId);
	      
	      Label label_5 = new Label("Name:");
	      label_5.setBounds(10, 48, 44, 22);
	      panel_8.add(label_5);
	      
	      final TextField textField_customerName = new TextField();
	      textField_customerName.setBounds(93, 48, 142, 22);
	      panel_8.add(textField_customerName);
	      
	      Label label_6 = new Label("Surname:");
	      label_6.setBounds(10, 76, 62, 22);
	      panel_8.add(label_6);
	      
	      final TextField textField_customerSurname = new TextField();
	      textField_customerSurname.setBounds(94, 76, 141, 22);
	      panel_8.add(textField_customerSurname);
	      
	      Label label_7 = new Label("Address:");
	      label_7.setBounds(10, 104, 62, 22);
	      panel_8.add(label_7);
	      
	      final TextField textField_customerAddress = new TextField();
	      textField_customerAddress.setBounds(94, 104, 341, 22);
	      panel_8.add(textField_customerAddress);
	      
	      Label label_8 = new Label("Email:");
	      label_8.setBounds(244, 20, 49, 22);
	      panel_8.add(label_8);
	      
	      final TextField textField_customerEmail = new TextField();
	      textField_customerEmail.setBounds(309, 20, 126, 22);
	      panel_8.add(textField_customerEmail);
	      
	      Label label_10 = new Label("Phone:");
	      label_10.setBounds(241, 48, 52, 22);
	      panel_8.add(label_10);
	      
	      final TextField textField_customerPhone = new TextField();
	      textField_customerPhone.setBounds(309, 48, 126, 22);
	      panel_8.add(textField_customerPhone);
	      
	      Label label_11 = new Label("Company:");
	      label_11.setBounds(241, 76, 62, 22);
	      panel_8.add(label_11);
	      
	      final TextField textField_customerCompany = new TextField();
	      textField_customerCompany.setBounds(309, 76, 126, 22);
	      panel_8.add(textField_customerCompany);
	      Icon save_customer = new ImageIcon("Images/disk.png");
	      JButton button = new JButton(save_customer);
	      button.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		
				int customerId = Integer.valueOf(textField_customerId.getText());
				String name = String.valueOf(textField_customerName.getText());
				String surname = String.valueOf(textField_customerSurname.getText());
				String address = String.valueOf(textField_customerAddress.getText());
				String email = String.valueOf(textField_customerEmail.getText());
				String phone = String.valueOf(textField_customerPhone.getText());
				String company = String.valueOf(textField_customerCompany.getText());
			
				boolean existing = false;
				for ( int j = 0; j < dbCustomer.getAllCustomer().size() ; j++)
				{
					if ( dbCustomer.getAllCustomer().get(j).getCustomerId()== customerId)
					{
					 existing = true;
					}
				}
				if(existing == false)
				{
				ctrCustomer.insertNewCustomer(customerId, name, surname, address, email, phone, company);
				
				customerTableModel.insertRow(i, new Object[]{customerId, name, surname, address, email, phone, company});
				textField_3.setText("Customer "+name+" was added to the system.");
				}
				else 
				{
					textField_3.setText("CustomerID:" +customerId+ "is already used in the system, please select another id." );
				}
				existing = false;
				}
	      	
	      });
	      button.setToolTipText("Save Customer");
	      button.setBackground(SystemColor.activeCaption);
	      button.setBounds(41, 11, 34, 22);
	      panel_7.add(button);
	      
	      JPanel panel_9 = new JPanel();
	      panel_9.setBackground(SystemColor.activeCaption);
	      panel_9.setBorder(new TitledBorder(null, "Customer list", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_9.setBounds(4, 208, 1003, 411);
	      panel_7.add(panel_9);
	      panel_9.setLayout(null);
	      
	      
	      //customer list code start
	      
	      JScrollPane scrollPane_1 = new JScrollPane();
	      scrollPane_1.setBounds(10, 21, 983, 379);
	      panel_9.add(scrollPane_1);
	      
	      Object[][] customerData = null;
	      Object[] columnsNames = {"Customer Id", "Name", "Surname", "Address", "Email","Phone","Company"};
	      customerTableModel = new DefaultTableModel(customerData,columnsNames);
	      customerTableList = new JTable(customerTableModel);
	      customerTableList.addMouseListener(new MouseAdapter() 
	      {
	      	@Override
	      	public void mouseClicked(MouseEvent e) 
	      	{
	      		int row = customerTableList.getSelectedRow();
	      		int customerId = Integer.parseInt(String.valueOf(customerTableModel.getValueAt(row, 0)));
	      		ctrCustomer.findCustomer(customerId);
	      		textField_customerId.setText(Integer.toString(customerId));
	      		textField_customerName.setText(ctrCustomer.findCustomer(customerId).getCustomerName());
	      		textField_customerSurname.setText(ctrCustomer.findCustomer(customerId).getCustomerSurname());
	      		textField_customerAddress.setText(ctrCustomer.findCustomer(customerId).getCustomerAddress());
	      		textField_customerEmail.setText(ctrCustomer.findCustomer(customerId).getCustomerEmail());
	      		textField_customerPhone.setText(ctrCustomer.findCustomer(customerId).getCustomerPhone());
	      		textField_customerCompany.setText(ctrCustomer.findCustomer(customerId).getCustomerCompany());
	      		
	      		
	      	}
	      });
	      scrollPane_1.setViewportView(customerTableList);
	      for( i = 0; i< customerList.size();i++)
	      {
	    	  customerTableModel.insertRow(i, new Object[]{customerList.get(i).getCustomerId(),customerList.get(i).getCustomerName(), customerList.get(i).getCustomerSurname(), customerList.get(i).getCustomerAddress(), customerList.get(i).getCustomerEmail(),customerList.get(i).getCustomerPhone(), customerList.get(i).getCustomerCompany() });
	      }
	      Icon delete_customer = new ImageIcon("Images/ico_down.png");
	      JButton button_1 = new JButton(delete_customer);
	      button_1.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		try {
	      		int row = customerTableList.getSelectedRow();
	      		int customerId = Integer.parseInt(String.valueOf(customerTableModel.getValueAt(row, 0)));
	      		ctrCustomer.deleteCustomer(customerId);
	      		customerTableModel.removeRow(row);
	      		i = i - 1;
	      		textField_customerAddress.setText(null);
	      		textField_customerCompany.setText(null);
	      		textField_customerEmail.setText(null);
	      		textField_customerId.setText(null);
	      		textField_customerName.setText(null);
	      		textField_customerPhone.setText(null);
	      		textField_customerSurname.setText(null);
	      		textField_3.setText("Customer with customerId:"+customerId+" was deleted from the system.");
	      		}catch(Exception q){textField_3.setText("You must select customer from customer list to delete it, please try again.");
	      		}
	      		}
	      		
	      	
	      });
	      button_1.setToolTipText("Delete customer");
	      button_1.setBackground(SystemColor.activeCaption);
	      button_1.setBounds(107, 11, 34, 22);
	      panel_7.add(button_1);
	      ;
	      Icon new_customer = new ImageIcon("Images/icon_new.png");
	      JButton button_2 = new JButton(new_customer);
	      button_2.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent arg0) 
	      	{
	      		textField_customerAddress.setText(null);
	      		textField_customerCompany.setText(null);
	      		textField_customerEmail.setText(null);
	      		textField_customerId.setText(null);
	      		textField_customerName.setText(null);
	      		textField_customerPhone.setText(null);
	      		textField_customerSurname.setText(null);
	      	}
	      });
	      button_2.setToolTipText("Create customer");
	      button_2.setBackground(SystemColor.activeCaption);
	      button_2.setBounds(8, 11, 34, 22);
	      panel_7.add(button_2);
	      
	      Icon update_customer = new ImageIcon("Images/disk_red.png");
	      JButton button_3 = new JButton(update_customer);
	      button_3.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e)
	      	{
	      		int row = customerTableList.getSelectedRow();
	      		int customerId = Integer.valueOf(textField_customerId.getText());
				String name = String.valueOf(textField_customerName.getText());
				String surname = String.valueOf(textField_customerSurname.getText());
				String address = String.valueOf(textField_customerAddress.getText());
				String email = String.valueOf(textField_customerEmail.getText());
				String phone = String.valueOf(textField_customerPhone.getText());
				String company = String.valueOf(textField_customerCompany.getText());
				customerTableModel.setValueAt(name, row , 1);
				customerTableModel.setValueAt(surname, row , 2);
				customerTableModel.setValueAt(address, row , 3);
				customerTableModel.setValueAt(email, row , 4);
				customerTableModel.setValueAt(phone, row , 5);
				customerTableModel.setValueAt(company, row , 6);
	      		ctrCustomer.updateCustomer(customerId, name, surname, address, email, phone, company);
	      		
	      	}
	      });
	      button_3.setToolTipText("Update customer");
	      button_3.setBackground(SystemColor.activeCaption);
	      button_3.setBounds(74, 11, 34, 22);
	      panel_7.add(button_3);
	      
	      JPanel panel_11 = new JPanel();
	      panel_11.setBackground(SystemColor.inactiveCaption);
	      tabbedPane.addTab("Materials & Labour", null, panel_11, null);
	      panel_11.setLayout(null);
	      
	      JPanel panel_12 = new JPanel();
	      panel_12.setBackground(SystemColor.activeCaption);
	      panel_12.setBorder(new TitledBorder(null, "New Material", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_12.setBounds(4, 47, 360, 100);
	      panel_11.add(panel_12);
	      panel_12.setLayout(null);
	      
	      
	      
	      
	      
	      final TextField textField_materialId = new TextField();
	      textField_materialId.setBounds(74, 16, 85, 22);
	      panel_12.add(textField_materialId);
	      
	      final TextField textField_materialName = new TextField();
	      textField_materialName.setBounds(74, 44, 276, 22);
	      panel_12.add(textField_materialName);
	      
	      final TextField textField_materialType = new TextField();
	      textField_materialType.setBounds(301, 16, 49, 22);
	      panel_12.add(textField_materialType);
	      
	      Icon new_material = new ImageIcon("Images/icon_new.png");
	      JButton button_4 = new JButton(new_material);
	      button_4.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		textField_materialId.setText(null);
	      		textField_materialName.setText(null);
	      		textField_materialType.setText(null);
	      	}
	      });
	      button_4.setToolTipText("Create material");
	      button_4.setBackground(SystemColor.activeCaption);
	      button_4.setBounds(4, 11, 34, 22);
	      panel_11.add(button_4);
	      
	      Icon save_material = new ImageIcon("Images/disk.png");
	      JButton button_5 = new JButton(save_material);
	      button_5.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int materialId = Integer.valueOf(textField_materialId.getText());
				String name = String.valueOf(textField_materialName.getText());
				String type = String.valueOf(textField_materialType.getText());
				boolean existing = false;
				for ( int j = 0; j < dbMaterial.getAllMaterials().size() ; j++)
				{
					if ( dbMaterial.getAllMaterials().get(j).getMaterialId()== materialId)
					{
					 existing = true;
					}
				}
				if(existing == false)
				{
				ctrMaterial.insertNewMaterial(materialId, name, type);
				materialTableModel.insertRow(p, new Object[]{materialId, name, type});
				textField_3.setText("Material "+name+" was added to the system.");
				}
				else
				{
					textField_3.setText("Material Id:"+materialId+" is aleady used in the system, pelase select aother id.");
				}
	      	}
	      });
	      button_5.setToolTipText("Save material");
	      button_5.setBackground(SystemColor.activeCaption);
	      button_5.setBounds(38, 11, 34, 22);
	      panel_11.add(button_5);
	      
	      
	      Icon update_material = new ImageIcon("Images/disk_red.png");
	      JButton button_6 = new JButton(update_material);
	      button_6.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int row = materialsTableList.getSelectedRow();
	      		int materialId = Integer.valueOf(textField_materialId.getText());
				String name = String.valueOf(textField_materialName.getText());
				String type = String.valueOf(textField_materialType.getText());
				ctrMaterial.updateMaterial(materialId, name, type);
				materialTableModel.setValueAt(name, row , 1);
				materialTableModel.setValueAt(type, row , 2);
				textField_3.setText("Material with material id:"+materialId+ " was successfully updated.");
	      	}
	      });
	      button_6.setToolTipText("Update material");
	      button_6.setBackground(SystemColor.activeCaption);
	      button_6.setBounds(72, 11, 34, 22);
	      panel_11.add(button_6);
	      
	      Icon delete_material = new ImageIcon("Images/ico_down.png");
	      JButton button_7 = new JButton(delete_material);
	      button_7.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		try{
	      		int row = materialsTableList.getSelectedRow();
	      		int materialId = Integer.valueOf(textField_materialId.getText());
	      		ctrMaterial.deleteMaterial(materialId);
	      		materialTableModel.removeRow(row);
	      		p = p - 1;
	      		textField_materialId.setText(null);
	      		textField_materialName.setText(null);
	      		textField_materialType.setText(null);
	      		textField_3.setText("Material:"+materialId+" was deteted form the system");
	      		}catch(Exception q){textField_3.setText("You must select material from material list to delete it, pelase try again.");
		      	}
	      	}
	      });
	      button_7.setToolTipText("Delete material");
	      button_7.setBackground(SystemColor.activeCaption);
	      button_7.setBounds(105, 11, 34, 22);
	      panel_11.add(button_7);
	      
	      
	      
	      Label label_15 = new Label("Material Id:");
	      label_15.setBounds(6, 16, 62, 22);
	      panel_12.add(label_15);
	      
	      
	      Label label_17 = new Label("Type:");
	      label_17.setBounds(254, 16, 41, 22);
	      panel_12.add(label_17);
	      
	      JLabel lblNewLabel = new JLabel("Name:");
	      lblNewLabel.setBounds(6, 52, 46, 14);
	      panel_12.add(lblNewLabel);

	      JPanel panel_15 = new JPanel();
	      panel_15.setBackground(SystemColor.activeCaption);
	      panel_15.setBorder(new TitledBorder(null, "Materials List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_15.setBounds(4, 158, 499, 459);
	      panel_11.add(panel_15);
	      panel_15.setLayout(null);
	      
	      JScrollPane scrollPane_2 = new JScrollPane();
	      scrollPane_2.setBounds(10, 27, 479, 421);
	      panel_15.add(scrollPane_2);
	      
	      Object[][] materialData = null;
	      Object[] columnsNamesMaterial = {"Material Id", "Name", "Type"};
	      materialTableModel = new DefaultTableModel(materialData,columnsNamesMaterial);
	      materialsTableList = new JTable(materialTableModel);
	      materialsTableList.addMouseListener(new MouseAdapter() {
	      	@Override
	      	public void mouseClicked(MouseEvent e) 
	      	{
	      		int row = materialsTableList.getSelectedRow();
	      		int materialId = Integer.parseInt(String.valueOf(materialTableModel.getValueAt(row, 0)));
	      		ctrMaterial.findMaterial(materialId);
	      		textField_materialId.setText(Integer.toString(materialId));
	      		textField_materialName.setText(ctrMaterial.findMaterial(materialId).getMaterialName());
	      		textField_materialType.setText(ctrMaterial.findMaterial(materialId).getMaterialType());
	      	}
	      });
	      scrollPane_2.setViewportView(materialsTableList);
	      for( p = 0; p< materialList.size();p++)
	      {
	    	  materialTableModel.insertRow(p, new Object[]{materialList.get(p).getMaterialId(),materialList.get(p).getMaterialName(),materialList.get(p).getMaterialType()});
	      }
	      
	      JPanel panel_17 = new JPanel();
	      panel_17.setBackground(SystemColor.activeCaption);
	      panel_17.setBorder(new TitledBorder(null, "New Labour", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_17.setBounds(508, 47, 382, 100);
	      panel_11.add(panel_17);
	      panel_17.setLayout(null);
	      
	      Label label_20 = new Label("Labour Id:");
	      label_20.setBounds(6, 16, 62, 22);
	      panel_17.add(label_20);
	      
	      final TextField textField_labourId = new TextField();
	      textField_labourId.setBounds(74, 16, 85, 22);
	      panel_17.add(textField_labourId);
	      
	      Label label_21 = new Label("Name:");
	      label_21.setBounds(6, 44, 62, 22);
	      panel_17.add(label_21);
	      
	      final TextField textField_labourName = new TextField();
	      textField_labourName.setBounds(74, 44, 298, 22);
	      panel_17.add(textField_labourName);
	      
	      JPanel panel_14 = new JPanel();
	      panel_14.setBackground(SystemColor.activeCaption);
	      panel_14.setBorder(new TitledBorder(null, "Labour List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	      panel_14.setBounds(508, 158, 499, 459);
	      panel_11.add(panel_14);
	      panel_14.setLayout(null);
	      
	      JScrollPane scrollPane_3 = new JScrollPane();
	      scrollPane_3.setBounds(10, 28, 479, 420);
	      panel_14.add(scrollPane_3);
	      
	      Object[][] labourData = null;
	      Object[] columnsNamesLabour1 = {"Labour Id ", "Name"};
	      labourTableModel = new DefaultTableModel(labourData,columnsNamesLabour1);
	      labourTableList = new JTable(labourTableModel);
	      labourTableList.addMouseListener(new MouseAdapter() 
	      {
	      	@Override
	      	public void mouseClicked(MouseEvent e) 
	      	{
	      		int row = labourTableList.getSelectedRow();
	      		int labourId = Integer.parseInt(String.valueOf(labourTableModel.getValueAt(row, 0)));
	      		ctrLabour.findLabour(labourId);
	      		textField_labourId.setText(Integer.toString(labourId));
	      		textField_labourName.setText(ctrLabour.findLabour(labourId).getLabourName());
	      	}
	      });
	      scrollPane_3.setViewportView(labourTableList);
	      
	      for( o = 0; o < labourList.size();o++)
	      {
	    	  labourTableModel.insertRow(o, new Object[]{labourList.get(o).getLabourID(), labourList.get(o).getLabourName()});
	      }
	      
	      Icon new_labour = new ImageIcon("Images/icon_new.png");
	      JButton button_8 = new JButton(new_labour);
	      button_8.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e)
	      	{
	      		textField_labourId.setText(null);
	      		textField_labourName.setText(null);
	      	}
	      });
	      button_8.setToolTipText("Create labour");
	      button_8.setBackground(SystemColor.activeCaption);
	      button_8.setBounds(508, 11, 34, 22);
	      panel_11.add(button_8);
	      
	      Icon save_labour = new ImageIcon("Images/disk.png");
	      JButton button_9 = new JButton(save_labour);
	      button_9.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int labourId = Integer.valueOf(textField_labourId.getText());
				String name = String.valueOf(textField_labourName.getText());
				boolean existing = false;
				for ( int j = 0; j < dbLabour.getAllLabour().size() ; j++)
				{
					if ( dbLabour.getAllLabour().get(j).getLabourID()== labourId)
					{
					 existing = true;
					}
				}
				if(existing == false)
				{
					ctrLabour.insertNewLabour(labourId, name);
					labourTableModel.insertRow(o, new Object[]{labourId, name});
					textField_3.setText("Labour was added to the system!");
				}
				else
				{
				textField_3.setText("LabourId:"+labourId+ " is already used in the system, please select different id.");
				}
				
	      	}
	      
	      });
	      button_9.setToolTipText("Save labour");
	      button_9.setBackground(SystemColor.activeCaption);
	      button_9.setBounds(540, 11, 34, 22);
	      panel_11.add(button_9);
	      
	      Icon update_labour = new ImageIcon("Images/disk_red.png");
	      JButton button_10 = new JButton(update_labour);
	      button_10.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		int row = labourTableList.getSelectedRow();
	      		int labourId = Integer.valueOf(textField_labourId.getText());
				String name = String.valueOf(textField_labourName.getText());
				ctrLabour.updateLabour(labourId, name);
				labourTableModel.setValueAt(name, row , 1);
				textField_3.setText("Labour with labour id:"+labourId+ " was successfully updated.");
	      	}
	      });
	      button_10.setToolTipText("Update ");
	      button_10.setBackground(SystemColor.activeCaption);
	      button_10.setBounds(573, 11, 34, 22);
	      panel_11.add(button_10);
	      
	      Icon delete_labour = new ImageIcon("Images/ico_down.png");
	      JButton button_11 = new JButton(delete_labour);
	      button_11.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) 
	      	{
	      		try{
	      		int row = labourTableList.getSelectedRow();
	      		int labourId = Integer.parseInt(String.valueOf(labourTableModel.getValueAt(row, 0)));
	      		labourTableModel.removeRow(row);
	      		o = o - 1;
				ctrLabour.deleteLabour(labourId);
				textField_labourId.setText(null);
				textField_labourName.setText(null);
				textField_3.setText("Labour:"+labourId+" was deteted form the system");
	      		}catch(Exception q){textField_3.setText("You must select labour from labour list to delete it, pelase try again.");
	      	}
	      }
	      });
	      button_11.setToolTipText("Delete labour");
	      button_11.setBackground(SystemColor.activeCaption);
	      button_11.setBounds(606, 11, 34, 22);
	      panel_11.add(button_11);
	      
	      Label label_9 = new Label("Logged as:");
	      label_9.setBounds(16, 707, 62, 22);
	      contentPane.add(label_9);
	      
	      TextField textField_4 = new TextField();
	      textField_4.setEditable(false);
	      textField_4.setBounds(84, 707, 160, 22);
	      contentPane.add(textField_4);
	      
	      checkbox_1 = new Checkbox("Materials Included");
	      checkbox_1.addItemListener(new ItemListener() {
	      	public void itemStateChanged(ItemEvent e) 
	      	{
	      	
	      		checkbox_1.getState();
	      		if(checkbox_1.getState() == false)
	      		{
	      			textField_materialAmount.setEditable(false);
	      			textField_materialAmount.setEnabled(false);
	      			textField_materialPrice.setEditable(false);
	      			textField_materialPrice.setEnabled(false);
	      			product_add.setEnabled(false);
	      			button_15.setEnabled(false);
	      			product_delete.setEnabled(false);
	      			comboBoxMaterial.setEnabled(false);
	      			
	      		}
	      		else
	      		{
	      			textField_materialAmount.setEditable(true);
	      			textField_materialAmount.setEnabled(true);
	      			textField_materialPrice.setEditable(true);
	      			textField_materialPrice.setEnabled(true);
	      			product_add .setEnabled(true);
	      			button_15.setEnabled(true);
	      			product_delete.setEnabled(true);
	      			comboBoxMaterial.setEnabled(true);
	      			
	      		
	      		}
	      		
	      	}
	      });
	      checkbox_1.setBounds(10, 97, 141, 22);
	      panel_2.add(checkbox_1);
	     
	}
}
